package cn.car.schedule;

import cn.car.service.CameraSnapshotService;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class CameraSnapshotSchedule {

    @Resource
    private CameraSnapshotService cameraSnapshotService;

    /**
     * 定时删除5分钟前的照片
     */
    @Scheduled(fixedRate = 1,timeUnit = TimeUnit.MINUTES,initialDelay = 0)
    public void deleteImage() {

        log.info("开始定时删除照片定时");

        //计算要删除的时间
        Date offset = DateUtil.offset(new Date(), DateField.MINUTE, -3);

        //查询
        List<Long> ids = cameraSnapshotService.selectBeforeDate(offset);

        //删除
        cameraSnapshotService.deleteImage(ids);

        log.info("结束定时删除照片定时 一共删除{}张照片", ids.size());
    }
}
